package net.yla.smartbus.module.ba.right.setting.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.yla.smartbus.core.api.RequestCode;
import net.yla.smartbus.core.base.BaseService;
import net.yla.smartbus.core.exception.AppException;
import net.yla.smartbus.core.util.DtoUtils;
import net.yla.smartbus.module.ba.right.menu.entity.MenusDO;
import net.yla.smartbus.module.ba.right.setting.query.SettingQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import net.yla.smartbus.module.ba.right.setting.entity.SettingsDO;
import net.yla.smartbus.module.ba.right.setting.mapper.SettingsMapper;
import net.yla.smartbus.module.ba.right.setting.service.SettingsService;

/**
 * @Date: 2020-08-29 9:36
 * @Author: Being Nobody
 * @Version: 1.0
 */
@Service
public class SettingsServiceImpl extends BaseService implements SettingsService {

    @Resource
    private SettingsMapper settingsMapper;

    @Override
    public int deleteByPrimaryKey(Integer settingsId) {
        return settingsMapper.deleteByPrimaryKey(settingsId);
    }

    @Override
    public int insert(SettingsDO record) {
        return settingsMapper.insert(record);
    }

    @Override
    public int insertSelective(SettingsDO record) {
        return settingsMapper.insertSelective(record);
    }

    @Override
    public SettingsDO selectByPrimaryKey(Integer settingsId) {
        return settingsMapper.selectByPrimaryKey(settingsId);
    }

    @Override
    public int updateByPrimaryKeySelective(SettingsDO record) {
        return settingsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SettingsDO record) {
        return settingsMapper.updateByPrimaryKey(record);
    }

    /**
     * 获取系统配置值
     *
     * @param query
     * @return
     */
    @Override
    public JSONObject get(SettingQuery query) {
        SettingsDO settingsDO = settingsMapper.selectByUnionKey(query);
        JSONObject jsonObject =null;
        if (settingsDO != null) {
            jsonObject = new JSONObject();
            jsonObject.put("value",settingsDO.getValue());
            jsonObject.put("type",settingsDO.getDataType());
        }
        return jsonObject;
    }

    /**
     * 设置系统配置设置
     *
     * @param query
     * @return
     */
    @Override
    public int set(SettingQuery query) {
        JSONObject jsonObject = DtoUtils.asDtoJsonForOnly(query);
        SettingsDO settingsDO = JSON.toJavaObject(jsonObject, SettingsDO.class);
        if (settingsMapper.updateByUnionKey(settingsDO)<1) {
            throw new AppException("更新失败", RequestCode.Operate_Tip);
        }
        return 1;
    }

}
